package org.eenie.kdassistant.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.eenie.kdassistant.App;
import org.eenie.kdassistant.ui.ExpressEntity;
import org.eenie.kdassistant.ui.ExpressEntityDao;
import org.eenie.kdassistant.ui.scan.ScanActivity;
import org.eenie.kdassistant.utils.LogUtil;

/**
 * Created by Eenie on 2017/8/7.
 * Email: eeniework@gmail.com
 * Des:
 */

public class ExpressStateProvider extends ContentProvider {


    @Override
    public boolean onCreate() {
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {

        LogUtil.e("query = " + uri.toString());

        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {

        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {

        LogUtil.e("insert = " + uri.toString());
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {

        LogUtil.e("delete = " + uri.toString());
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        LogUtil.e("update = " + uri.toString());
        ExpressEntityDao expressEntityDao = ((App) getContext().getApplicationContext()).getApplicationComponent().getDaoSession().getExpressEntityDao();
        ExpressEntity entity = expressEntityDao.queryBuilder().where(ExpressEntityDao.Properties.Express.eq(selectionArgs[0])).build().unique();
        entity.setState(values.getAsInteger("state"));
        entity.setState_des(values.getAsString("state_des"));
        expressEntityDao.update(entity);
        getContext().sendBroadcast(new Intent(ScanActivity.BROADCAST_ACTION_EXPRESS_UPDATE)
                .putExtra("express", selectionArgs[0]));
        return 0;
    }
}
